<?php
session_cache_expire(999999999);
if(!isset($_SESSION))
{
	session_start();
}
?>
<link	href="css/style.css" rel="stylesheet" type="text/css" />
<?php 
$nowdate=date("Y-m-d");
$usdate=getdate(strtotime($nowdate));

if(isset($_REQUEST['months'])){
	$vmonth=$_REQUEST['months'];
}else{
	$vmonth=date('n');
}

if(isset($_REQUEST['years'])){
	$vyear=$_REQUEST['years'];
}else{
	$vyear=$usdate[year];
}
$_SESSION['thongbao_employee_salary']="";
include 'operations/connection.php';
include "generallibraries/funcs.php";
$selectyear = mysql_query("SELECT EXTRACT(YEAR FROM sac.time_work) as years FROM sa_check_time sac GROUP BY years");
//$selectmonth = mysql_query("SELECT EXTRACT(MONTH FROM sac.time_work) as months FROM sa_check_time sac GROUP BY months");
/*$query_de = mysql_query("select department.IDDepartment, department_lang.DepartmentName from department, department_lang where department.IDDepartment = department_lang.IDDepartment and department_lang.LangID='".$_SESSION['lang']."'");
$mangkq=array();
while ($row=mysql_fetch_array($query_de)){
	$query11  = mysql_query("SELECT IDEmployee,FullName  FROM employee where State!=0 and IDDepartment='".$row[0]."'");
	$mangone=array();
	while($row1=mysql_fetch_array($query11)){
		$mangone[]=$row1;
	}
	$mangkq[$row[1]]=$mangone;
}
$bien = generalFunctions::xuatds($mangkq,'empl',$l_all);*/

	$mqd=array();
	$mc=array();
	$mem=array();
	$_SESSION['chung']=array();
	$_SESSION['quidinh']=array();
	$_SESSION['emsa']=array();
	$_REQUEST['txtbonus']=0;
	
	$s1="SELECT is_late,is_half_dayoff,is_all_dayoff,allowance_hard,allowance_holiday,sub_salary_day 
	from sa_allowance_hard_holiday where sa_allowance_hard_holiday.is_status=1";
	$ms1=mysql_query($s1);
	if(mysql_num_rows($ms1)>0){
		while($row=mysql_fetch_array($ms1)){
			$tem=array();
			$tem[is_late]=$row[is_late];
			$tem[is_half_dayoff]=$row[is_half_dayoff];
			$tem[is_all_dayoff]=$row[is_all_dayoff];
			$tem[allowance_hard]=$row[allowance_hard];
			$tem[allowance_holiday]=$row[allowance_holiday];
			$tem[sub_salary_day]=$row[sub_salary_day];
			$mqd[]=$tem;
		}
		$_SESSION['quidinh']=$mqd;
		//print_r($_SESSION['quidinh']);
	}
	
	$s2="SELECT coefficient_insurance,number_time_work,number_max_holiday,allowance_holiday,allowance_lunch,allowance_car,allowance_hard,allowance_overtime 
	from sa_general_allowance where sa_general_allowance.is_status=1";
	$ms2=mysql_query($s2);
	if(mysql_num_rows($ms2)>0){
		while($row=mysql_fetch_array($ms2)){
			$mc[number_time_work]=$row[number_time_work];
			$mc[number_max_holiday]=$row[number_max_holiday];
			$mc[allowance_holiday]=$row[allowance_holiday];
			$mc[allowance_lunch]=$row[allowance_lunch];
			$mc[allowance_car]=$row[allowance_car];
			$mc[allowance_hard]=$row[allowance_hard];
			$mc[allowance_overtime]=$row[allowance_overtime];
			$mc[coefficient_insurance]=$row[coefficient_insurance];
		}
		$_SESSION['chung']=$mc;
		//print_r($_SESSION['chung']);
		/*	echo $_SESSION['chung'][number_time_work]."<br>";
			echo $_SESSION['chung'][number_max_holiday]."<br>";
			echo $_SESSION['chung'][allowance_holiday]."<br>";
			echo $_SESSION['chung'][allowance_lunch]."<br>";
			echo $_SESSION['chung'][allowance_car]."<br>";
			echo $_SESSION['chung'][allowance_hard]."<br>";
			echo $_SESSION['chung'][allowance_overtime]."<br>"; */
	}

	$ktview=1;
	$thang=$_REQUEST['months'];
	$nam=$_REQUEST['years'];
	//$nhanvien=$_REQUEST['empl'];
	$idlog=$nam."-".$thang;
	$tinh_trang_bang_luong=$l_unsaved;
	$nguoitao=$_SESSION['fullname'];
	$ngaytao=date("Y-m-d");
	$nguoicn="";
	$ngaycn="";
	
	/*$ktquery="SELECT sa_employee_salary_log.idlog 
	from sa_employee_salary_log 
	where sa_employee_salary_log.idlog='".$idlog."'";*/
	
	$ktquery="SELECT sa_employee_salary_log.idlog,
	sa_employee_salary_log.date_create,
	sa_employee_salary_log.date_last_update,
	sa_employee_salary_log.is_saved,uc.FullName as uscreate, lu.FullName as uslastupdate  
	from sa_employee_salary_log left JOIN employee uc on sa_employee_salary_log.user_create=uc.IDEmployee 
	left join employee lu on sa_employee_salary_log.user_last_update=lu.IDEmployee 
	where sa_employee_salary_log.idlog='".$idlog."'";
	//echo $ktquery;
	$mktquery=mysql_query($ktquery);
	if(mysql_num_rows($mktquery)>0){
		$ktview=0;
		while($r=mysql_fetch_array($mktquery)){
			if($r[is_saved]==0){
				$tinh_trang_bang_luong=$l_temporary_storage;
			}else{
				$tinh_trang_bang_luong=$l_confirm_and_save;
			}
			$nguoitao=$r[uscreate];
			$ngaytao=$r[date_create];
			$nguoicn=$r[uslastupdate];
			$ngaycn=$r[date_last_update];
		}
	}
	
if(isset($_REQUEST['btlapbangluong'])){
	if($ktview==0){
		$query="SELECT sa.*, e.FullName, e.Position,e.IDEmployee,e.Type_employee,e.StartDate,d.DepartmentName   
		from sa_employee_salary sa , employee e,department_lang d
		where e.IDEmployee=sa.employeeID and e.IDDepartment=d.IDDepartment AND d.LangID='$lang' ";
		if($thang !="" && $nam!=""){
			$query .=" and sa.sa_time='".$idlog."'";
		}
		/*if($nhanvien!="ALL"){
			$query .=" and  employee.IDEmployee='".$nhanvien."'";
		}*/
		$query .=" and e.State=1 order by  e.IDEmployee asc";
		//echo $query;
		$result=mysql_query($query);
		if(mysql_num_rows($result)>0){
			while($row=mysql_fetch_array($result)){
				$t=array();
				$t[employeeID]=$row[IDEmployee];
				$t[name]=$row[FullName];
				$t[position]=$row[DepartmentName];
				$t[number_time_work]=$row[number_time_work];
				$t[really_number_time_work]=$row[really_number_time_work];
				$t[number_allow_holiday]=$row[number_allow_holiday];
				$t[basic_salary]=$row[basic_salary];
				$t[allowance_basic_salary]=$row[allowance_basic_salary];
				
				$t[number_dateoff]=$row[number_dateoff];
				$t[number_holiday]=$row[number_holiday];
				
				$t[number_is_comeback_early]=$row[number_is_comeback_early];
				
				$t[number_is_late]=$row[number_is_late];
				$t[allowance_senior]=$row[allowance_senior];
				$t[allowance_c]=$row[allowance_c];
				
				$t[oneday_salary]=$row[oneday_salary];
				
				$t[paid_salary]=$row[paid_salary];
				
				$t[allowance_holiday]=$row[allowance_holiday];
				$t[allowance_lunch]=$row[allowance_lunch];
				$t[allowance_car]=$row[allowance_car];
				
				$t[allowance_hard]=$row[allowance_hard];
				
				
				$t[number_overtime]=$row[number_overtime];
				$t[overtime_salary]=$row[overtime_salary];
				
				$t[total_salary]=$row[total_salary];
				
				$t[insurance]=$row[insurance];
				$t[salary_bornus]=$row[salary_bornus];
				$t[salary_sub]=$row[salary_sub];
				
				$t[received_salary]=$row[received_salary];
				
				$t[note]=$row[note];
				$t[is_saved]=$row[is_saved];
				$t[is_sendmail]=$row[is_sendmail];
				$t[Type_employee]=$row[Type_employee];
				
				
				$t[allowance_longyear]=$row[allowance_longyear];
				$t[StartDate] = $row['StartDate'];
				
				$mem[$row[IDEmployee]]=$t;
			}
			
		}
	}else{
		$query="SELECT e.Type_employee,e.IDEmployee,e.FullName,e.EmployeeCode,e.Position,e.StartDate,sr.salary_insurance, 
		sr.basic_salary,sr.allowance_basic_salary,sr.allowance_c,sr.allowance_senior,
		sum(sc.all_day_off) as v_all_day_off, 
		sum(sc.half_day_off) as v_half_day_off, 
		sum(sc.is_late) as v_is_late, 
		sum(sc.is_comeback_early) as v_is_comeback_early, 
		sum(sc.overtime) as v_overtime,count(sc.time_work) as v_time_work,d.DepartmentName    
		from employee e,sa_regulation_salary sr,sa_check_time sc,department_lang d    
		where e.State=1 
		and e.IDEmployee=sr.employeeID 
		and e.EmployeeCode=sc.employeeID and sc.`status`!=1 
		and e.IDDepartment=d.IDDepartment AND d.LangID='$lang'";
		
		if($thang !="" && $nam!=""){
			$query .=" and (MONTH(sc.time_work)='".$thang."' and YEAR(sc.time_work)='".$nam."')";
		}
		
		/*if($nhanvien!="ALL"){
			$query .=" and  employee.IDEmployee='".$nhanvien."'";
		}*/
		
		$query .=" GROUP BY e.IDEmployee,e.FullName,e.EmployeeCode,
		sr.basic_salary,sr.allowance_basic_salary,sr.allowance_c,e.Position,sr.allowance_senior,sr.salary_insurance order by  e.IDEmployee asc";
		//echo $lang;
		//echo $query;
		//echo $query;
		$result=mysql_query($query);
		
		if(mysql_num_rows($result)>0){
			while($row=mysql_fetch_array($result)){
				$t=array();
				$t[employeeID]=$row[IDEmployee];
				$t[name]=$row[FullName];
				$t[position]=$row[DepartmentName];
				$t[number_time_work]=$_SESSION['chung'][number_time_work];
				$t[really_number_time_work]=$row[v_time_work];
				$t[number_allow_holiday]=0;
				$t[basic_salary]=$row[basic_salary];
				$t[allowance_basic_salary]=$row[allowance_basic_salary];
				$t[all_day_off]=$row[v_all_day_off];
				$t[half_day_off]=$row[v_half_day_off];
				
				$solannghinuangay_vesom=0;
				//$vesom_nghinuangay=(int)($row[v_is_comeback_early]/2);
				//if($vesom_nghinuangay>0){
				//	$solannghinuangay_vesom=$vesom_nghinuangay*0.5;
				//}
				
				$t[solannghinuangay_vesom]=$solannghinuangay_vesom;
				
				$ngaynghi=$row[v_all_day_off]+($row[v_half_day_off]*0.5);
				
				
				if($ngaynghi==0){
					$t[number_holiday]=0;
					$t[number_dateoff]=$solannghinuangay_vesom  - $_SESSION['chung'][number_max_holiday];
					if($t[number_dateoff]<0)
						$t[number_dateoff]=0;
				}else{
					if($ngaynghi<=$_SESSION['chung'][number_max_holiday]){
						$t[number_holiday]=$ngaynghi;
						$t[number_dateoff]=$solannghinuangay_vesom  - $_SESSION['chung'][number_max_holiday];
						if($t[number_dateoff]<0)
							$t[number_dateoff]=0;
					}else{
						$t[number_holiday]=$_SESSION['chung'][number_max_holiday];
						$t[number_dateoff]=$ngaynghi + $solannghinuangay_vesom  - $_SESSION['chung'][number_max_holiday];
					}
				}
				
				$t[number_is_comeback_early]=$row[v_is_comeback_early];
				
				$t[number_is_late]=$row[v_is_late];
				$t[allowance_senior]=$row[allowance_senior];
				$t[allowance_c]=$row[allowance_c];
				
				$StartDate = $row['StartDate'];
			    $date2 = date("Y-m-d");
			
			    $diff = abs(strtotime($date2) - strtotime($StartDate));
			
			    $years = floor($diff / (365 * 60 * 60 * 24));
			    $months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
			    $days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
				if($years>=1){
					$t[allowance_longyear]=500000*$years;
					if($months>=6)
						$t[allowance_longyear]+=250000;
				}
				else{
				$t[allowance_longyear]=0;
				}
				$luongmotngay=($row[allowance_basic_salary]+$row[basic_salary]+$row[allowance_senior]+$row[allowance_c]+$t[allowance_longyear])/$_SESSION['chung'][number_time_work];
				$t[oneday_salary]=$luongmotngay;
				
				
				
				
				
				//$luongduocnhan=$luongmotngay * ($_SESSION['chung'][number_time_work] - $t[number_dateoff] - $solannghinuangay_vesom);
				$songaycong_le=$t[really_number_time_work] + $t[number_allow_holiday];
				if($songaycong_le > $_SESSION['chung'][number_time_work]){
					$songaycong_le=$_SESSION['chung'][number_time_work];
				}
				
				$luongduocnhan=$luongmotngay * ($songaycong_le - $t[number_dateoff]);
				
				$t[paid_salary]=$luongduocnhan;
				
				$t[ngaytinhphucap]=$songaycong_le - $t[number_dateoff];
				$t[ktngaytinhphucap]=$t[number_holiday]+$solannghinuangay_vesom;
				
				//$t[allowance_holiday]=($_SESSION['chung'][allowance_holiday] * ($_SESSION['chung'][number_max_holiday] - $t[number_holiday]))/$_SESSION['chung'][number_max_holiday];
				//$t[allowance_lunch]=($_SESSION['chung'][allowance_lunch] * ($_SESSION['chung'][number_time_work] - $t[number_dateoff])) / $_SESSION['chung'][number_time_work] ;
				//$t[allowance_car]=($_SESSION['chung'][allowance_car] * ($_SESSION['chung'][number_time_work] - $t[number_dateoff])) / $_SESSION['chung'][number_time_work] ;
				
				$t[allowance_holiday]=($_SESSION['chung'][allowance_holiday] * ($_SESSION['chung'][number_max_holiday] - $t[number_holiday]-$solannghinuangay_vesom))/$_SESSION['chung'][number_max_holiday];
				if($t[allowance_holiday]<0)
					$t[allowance_holiday]=0;
				if($t[ktngaytinhphucap]<=$_SESSION['chung'][number_max_holiday]){
					$t[allowance_lunch]=$_SESSION['chung'][allowance_lunch] * $t[ngaytinhphucap] / $_SESSION['chung'][number_time_work] ;
					$t[allowance_car]=$_SESSION['chung'][allowance_car] * $t[ngaytinhphucap] / $_SESSION['chung'][number_time_work] ;
				}else{
					$t[allowance_lunch]=$_SESSION['chung'][allowance_lunch] * ($t[ngaytinhphucap] - $t[solannghinuangay_vesom]) / $_SESSION['chung'][number_time_work] ;
					$t[allowance_car]=$_SESSION['chung'][allowance_car] * ($t[ngaytinhphucap] - $t[solannghinuangay_vesom]) / $_SESSION['chung'][number_time_work] ;
				}
				
				
				
				$solantre=(int)($row[v_is_late]);
				if($solantre >1){
					$tientre=50000 * $solantre ;
					
					$t[allowance_hard]=0;
					//$t[allowance_hard]-$row[v_is_comeback_early]*100000 -((int)($row[v_is_late]/2))*100000;
					
						$hard_tiencon= $_SESSION['chung'][allowance_hard] - $tientre;
						if($t[number_dateoff]==0){
							$t[allowance_hard]=$hard_tiencon;
							
						}else{
							if($t[number_dateoff]==0.5){
								$t[allowance_hard]=$hard_tiencon - 100000;
							}else if($t[number_dateoff]==1) {
								$t[allowance_hard]=$hard_tiencon - 200000;
							}else if($t[number_dateoff]==1.5) {
								$t[allowance_hard]=$hard_tiencon - 300000;
							}else if($t[number_dateoff]==2) {
								$t[allowance_hard]=$hard_tiencon - 400000;
								
							}else if($t[number_dateoff]==2.5) {
								$t[allowance_hard]=$hard_tiencon - 500000;
							}else{
								$t[allowance_hard]=0;
							}
						
						}
				}else{
					//$tientre=100000*$row[v_is_comeback_early];
					$hard_tiencon= $_SESSION['chung'][allowance_hard] - $tientre;
					if($t[number_dateoff]==0){
						$t[allowance_hard]=$_SESSION['chung'][allowance_hard];
					}else{
						if($t[number_dateoff]==0.5){
							$t[allowance_hard]=$_SESSION['chung'][allowance_hard] - 100000;
						}else if($t[number_dateoff]==1) {
							$t[allowance_hard]=$_SESSION['chung'][allowance_hard] - 200000;
						}else if($t[number_dateoff]==1.5) {
							$t[allowance_hard]=$_SESSION['chung'][allowance_hard] - 300000;
						}else if($t[number_dateoff]==2) {
							$t[allowance_hard]=$_SESSION['chung'][allowance_hard] - 400000;
						}else if($t[number_dateoff]==2.5) {
							$t[allowance_hard]=$_SESSION['chung'][allowance_hard] - 500000;
						}
						else{
							$t[allowance_hard]=0;
						}
					}
				}
				if($t[allowance_hard]<0){
					$t[allowance_hard]=0;
				}
				
				$vesom_nghinuangay=$row[v_is_comeback_early];
				if($vesom_nghinuangay!=0){
					$t[allowance_hard]=$t[allowance_hard] - 100000*$vesom_nghinuangay;
					if($t[allowance_hard]<0){
						$t[allowance_hard]=0;
					}
				}
				
				
				if($t[allowance_hard]<0){
					$t[allowance_hard]=0;
				}
				if($ngaynghi==0){
					$t[number_dateoff]=0;
					$t[number_holiday]=0;
				}else{
					if($ngaynghi<=$_SESSION['chung'][number_max_holiday]){
						$t[number_holiday]=$ngaynghi;
						$t[number_dateoff]=0;
					}else{
						$t[number_holiday]=$_SESSION['chung'][number_max_holiday];
						$t[number_dateoff]=$ngaynghi  - $_SESSION['chung'][number_max_holiday];
					}
				}
				
				$t[number_overtime]=$row[v_overtime];
				$t[overtime_salary]=$row[v_overtime] * $_SESSION['chung'][allowance_overtime];
				
				
				
				
			
			    //printf("%d $l_year, %d $l_month, %d $l_date\n", $years, $months, $days);
				
				
				
				$t[total_salary]=$t[paid_salary] + $t[allowance_holiday] + $t[allowance_lunch] + $t[allowance_car] + $t[allowance_hard] +$t[overtime_salary];
				
				$t[insurance]=$row[salary_insurance] * $_SESSION['chung'][coefficient_insurance] / 100;
				$t[salary_bornus]=0;
				$t[salary_sub]=0;
				$t[Type_employee]=$row[Type_employee];
				
				$t[received_salary]=$t[total_salary] + $t[salary_bornus] - $t[insurance] - $t[salary_sub];
				$t[note]="";
				$t[note_bonus]="";
				$t[note_holiday]="";
				
				if($t[Type_employee]==0){
					$t[received_salary]=$t[received_salary] * 80 /100;
					$t[note]=$l_luongthuviec;
				}
				
				$t[is_saved]=0;
				$t[is_sendmail]=0;
				
				
				
				
				
				
				$t[StartDate] = $row['StartDate'];
				$mem[$row[IDEmployee]]=$t;
			}
			//print_r($mem);
		}
	}
}else{
	$tinh_trang_bang_luong="";
	$nguoitao="";
	$ngaytao="";
	$nguoicn="";
	$ngaycn="";
}

$_SESSION['emsa']=$mem;
?>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.price_format.1.7.js"></script>
<script type="text/javascript" src="js/jquery.price_format.1.7.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	document.getElementById("txtholiday").focus();
	document.getElementById("txtholiday").select();
	$('#txtbonus').priceFormat({
		prefix: '',
		centsLimit: 0,
		centsSeparator: '',
		thousandsSeparator: ','
	});

	$(".hang").mouseover(function(){	
		$(this).addClass("nenhang");	
	});
	
	$(".hang").mouseout(function(){	
		$(this).removeClass("nenhang");	
	});
});

function noloveyou(){
	$(document).ready(function(){
		$('#txtbonus').priceFormat({
			prefix: '',
			centsLimit: 0,
			centsSeparator: '',
			thousandsSeparator: ','
		});

		$(".hang").mouseover(function(){	
			$(this).addClass("nenhang");	
		});
		
		$(".hang").mouseout(function(){	
			$(this).removeClass("nenhang");	
		});
	});
}

function save(nam,thang){
	hoi= confirm("<?php echo $l_stored_data_will_not_be_changed;?>"+"\n"+"<?php echo $l_areyousure;?>");
	if (hoi==true) {
		if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
	 		xmlhttp=new XMLHttpRequest();
	 	}else{// code for IE6, IE5
	 		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	 	}
	 	xmlhttp.onreadystatechange=function(){
			 	if (xmlhttp.readyState==4 && xmlhttp.status==200){
			 		document.getElementById("showtb").innerHTML=xmlhttp.responseText;
			 		var tb=document.getElementById("viewthongbao").value;
			 		if(tb!=""){
			 			if(tb=="l_payroll_saved"){
							$.ajax({url:"ajax/salary/sa_view_em_salary.php?thang="+thang+"&nam="+nam,success:function(result){
								$("#totalprocess").html(result);
								alert("<?php echo $l_payroll_saved; ?>");
					    	}});
			 			}else if(tb=="l_payroll_not_caculate_again"){
							alert("<?php echo $l_payroll_not_caculate_again;?>");
			 			}
			 			window.location="salary.php?content=employee_salaries";
			 		}
			 		noloveyou();
			 	}
		 	}
	 	xmlhttp.open("POST","ajax/salary/sa_add_employee_salary.php?hanhdong=add&thang="+thang+"&nam="+nam,true);
	 	xmlhttp.send();
	}else{
		return false;
	}
}

function enterTable(e,value,co){
    var key = window.event ? e.keyCode : e.which;
    if (key == 13 ||key == 8 || key == 46  || key == 37 || key == 39 || co=='0') {
    	if(key==13){
    		var j = 0;
    		var listID = new Array();
    		listID = listID.concat(["txtholiday","txtnoteholiday","txtbonus","txtnote"]);	
    		for (j = 0; j < listID.length; j++){	
    			if (listID[j] == value) {	
    				if (j == listID.length - 1) {
    					return false;
    				}
    				document.getElementById(listID[j + 1]).focus();
    				document.getElementById(listID[j + 1]).select();
    				return false; 
    			}
    		}
			return false;
    	}
		return true;	
    } else if ( key < 48 || key > 57 ) {
        return false;
    }
    else return true;
				
}

function bonus(so){
	hoi= confirm("<?php echo $l_areyousure;?>");
	if (hoi==true) {
		var tien=0;
		var note="";
		if(so==0){
			document.getElementById("txtbonus").value=0;
			document.getElementById("txtnote").value="";
		}else{
			tien=document.getElementById("txtbonus").value;
			note =document.getElementById("txtnote").value;
		}
		if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
	 		xmlhttp=new XMLHttpRequest();
	 	}else{// code for IE6, IE5
	 		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	 	}
	 	xmlhttp.onreadystatechange=function(){
			 	if (xmlhttp.readyState==4 && xmlhttp.status==200){
			 		document.getElementById("showsalary").innerHTML=xmlhttp.responseText;
			 		/*var tb=document.getElementById("viewthongbao").value;
			 		if(tb!=""){
			 			alert(tb);
			 		}*/
			 		noloveyou();
			 	}
		 	}
	 	xmlhttp.open("POST","ajax/salary/sa_add_bonus.php?tienthuong="+tien+"&note="+encodeURIComponent(note),true);
	 	xmlhttp.send();
	}
		
}

function holiday(so){
	hoi= confirm("<?php echo $l_areyousure;?>");
	if (hoi==true) {
		var tien=0;
		var note="";
		if(so==0){
			document.getElementById("txtholiday").value=0;
			document.getElementById("txtnoteholiday").value="";
		}else{
			tien=document.getElementById("txtholiday").value;
			note =document.getElementById("txtnoteholiday").value;
		}
		if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
	 		xmlhttp=new XMLHttpRequest();
	 	}else{// code for IE6, IE5
	 		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	 	}
	 	xmlhttp.onreadystatechange=function(){
			 	if (xmlhttp.readyState==4 && xmlhttp.status==200){
			 		document.getElementById("showsalary").innerHTML=xmlhttp.responseText;
			 		/*var tb=document.getElementById("viewthongbao").value;
			 		if(tb!=""){
			 			alert(tb);
			 		}*/
			 		noloveyou();
			 	}
		 	}
	 	xmlhttp.open("POST","ajax/salary/sa_add_holiday.php?ngayle="+tien+"&note="+encodeURIComponent(note),true);
	 	xmlhttp.send();
	}
		
}

function exportexcel(nam,thang){
	
}

function sendmail(nam,thang){
	var listid="";
	$("input[id='chon']").each(function(){
		if (this.checked) listid = listid+","+this.value;
    	})
	listid=listid.substr(1);	 
	if (listid=="") { alert("<?php echo $l_nochoose;?>"); return;}
	hoi= confirm("<?php echo $l_areyousure;?>");
	if (hoi==true) {
		if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
	 		xmlhttp=new XMLHttpRequest();
	 	}else{// code for IE6, IE5
	 		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	 	}
	 	xmlhttp.onreadystatechange=function(){
			 	if (xmlhttp.readyState==4 && xmlhttp.status==200){
			 		document.getElementById("showtb").innerHTML=xmlhttp.responseText;
			 		var tb=document.getElementById("viewthongbao").value;
			 		if(tb!=""){
			 			alert(tb);
			 		}else{
						alert("null");
			 		}
			 	}
		 	}
	 	xmlhttp.open("POST","ajax/salary/sa_sendmail.php?thang="+thang+"&nam="+nam+"&listid="+listid,true);
	 	xmlhttp.send();
	}
}
</script>
<form id="sa_f_employee_salary" name="nsa_f_employee_salary" method="post" action="">
<div id="contains">
	<div class="title" style="padding-top: 5px;font-weight: bold;"><?php echo $l_accountant;?> &rarr; <?php echo $l_ml_tinh_luong;?> &rarr; <?php echo $l_employee_salaries?></div>
	<div class="content_code">
		<div class="approval-right">
			<div style="width:820px;border: 1px solid silver; padding: 10px 0; margin-bottom: 30px; background:white">
				<table width="820px" style="padding:0 10px">
					<tr>
						<td><b><?php echo $l_month?></b> </td>
						<td><b><?php echo $l_year?></b> </td>
						<td></td>
					</tr>
					<tr>
						<td width="80px">
							<select style="width:50px;height:24px;font-size:12px;" id="months" name="months">
								<?php 
								$rowmonth=1;
								while ($rowmonth < 13){?>
									<option value="<?=$rowmonth?>"  
										<?php 
											if($vmonth==$rowmonth){
												?>
												selected="selected"
												<?php 
											}
										?>
									>
										<?php echo $rowmonth;?>
									</option>
								<?php 
										$rowmonth++;
								}?>
							</select> 
						</td>
						<td width="130px">
							<select style="width:100px;height:24px;font-size:12px;" id="years" name="years">
								<?php while ($rowyear = mysql_fetch_array($selectyear)){?>
									<option value="<?=$rowyear[years]?>" 
										<?php 
											if($vyear==$rowyear[years]){
												?>
												selected="selected"
												<?php 
											}
										?>
									>
										<?php echo $rowyear[years]?>
									</option>
								<?php }?>
							</select>
						</td>
						<td align="left">
							<input type="submit" id="btlapbangluong" name="btlapbangluong" value="<?php echo $l_View;?>"  class="btnupbg btnupload" />
						</td>
					</tr>
				</table>
			</div>
			<div id="totalprocess" style="width:100%;border: 1px solid silver; padding: 10px 0; margin-bottom: 30px;">
				<table style="width:820px;">
					<tr>
						<td><b><?php echo $l_salary_satus;?></b></td>
					</tr>
					<tr>
						<td>
							<table style="padding:10px 5px" width="100%; background:white;">
								<tr>
									<td class="tdfirst"><?php echo $l_time;?></td>
									<td colspan="3" class="in_testfield" style="background:white;" ><?php echo $thang."-".$nam;?></td>
								</tr>
								<tr>
									<td class="tdfirst"><?php echo $l_salary_satus;?></td>
									<td colspan="3" class="in_testfield" style="background:white;"  ><?=$tinh_trang_bang_luong?></td>
								</tr>
								<tr>
									<td class="tdfirst"><?php echo $l_create_by;?></td>
									<td class="in_testfield"  style="background:white;" ><?=$nguoitao?></td>
									<td class="tdfirst"><?php echo $l_first_create;?></td>
									<td class="in_testfield" style="background:white;"  ><?=$ngaytao?></td>
								</tr>
								<!--<tr>
									<td class="tdfirst"><?php echo $l_user_last_update;?></td>
									<td class="in_testfield"  style="background:white;" ><?=$nguoicn?></td>
									<td class="tdfirst"><?php echo $l_time_last_update;?></td>
									<td class="in_testfield" style="background:white;"  ><?=$ngaycn?></td>
								</tr>  -->
							</table>
						</td>
					</tr>
				</table>
			
			<div style=" max-width: 100%" id="showsalary">
				<table class="rf-dt" width="100%">
					<tr>
						<!-- <th rowspan="2" class="rf-dt-shdr-c"></th> -->
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_no;?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_employee_name?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_department?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_number_time_work?></th>
						<!--  <th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_num_allow_holiday?></th>-->
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_basicsalary?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_allowancebasicsalary?></th>
						
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_dayoff?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_absentmonth?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_comebackearly?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo  $l_comelately?></th>
						
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_allowance_longyear?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_allowanceSenior?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_allowanceC?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_salaryperday?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_salaryreceived?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_allowance_holiday?></th>
						<th colspan="2" class="rf-dt-shdr-c"><?php echo $l_allowance?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_allowance_hard?></th>
						<th colspan="2" class="rf-dt-shdr-c"><?php echo $l_allowance_overtime?></th>
						
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_total?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_socialInsurance?></th>
						<!--<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_salary_bonus?></th>
						 <th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_salary_sub?></th> -->
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_realreceived?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_note;?></th>
						<th rowspan="2" class="rf-dt-shdr-c"><?php echo $l_start_date;?></th>
					</tr>
					<tr>
						<th class="rf-dt-shdr-c"><?php echo $l_lunch?></th>
						<th class="rf-dt-shdr-c"><?php echo $l_parking?></th>
						<th class="rf-dt-shdr-c"><?php echo $l_numbersession?></th>
						<th class="rf-dt-shdr-c"><?php echo $l_total_amount?></th>
					</tr>
					<tr style="background: #93cddd; color: white">
						<!--  <td class="rf-dt-c"></td> -->
						<td class="rf-dt-c"></td>
						<td class="rf-dt-c">(2)</td>
						<td class="rf-dt-c">(3)</td>
						<td class="rf-dt-c">(4)</td>
						<!--<td class="rf-dt-c"></td>-->
						<td class="rf-dt-c">(5)</td>
						<td class="rf-dt-c">(6)</td>
						<td class="rf-dt-c">(7)</td>
						<td class="rf-dt-c">(8)</td>
						<td class="rf-dt-c">(9)</td>
						
						<td class="rf-dt-c">(10)</td>
						<td class="rf-dt-c">(11)</td>
						<td class="rf-dt-c">(12)</td>
						<td class="rf-dt-c">(13)</td>
						<td class="rf-dt-c">(14)=[(5)+(6) +(11)+(12)+(13)]/(4)</td>
						<td class="rf-dt-c">(15)=(14) x [(4)-(7)]</td>
						<td class="rf-dt-c">(16)</td>
						<td class="rf-dt-c">(17)</td>
						<td class="rf-dt-c">(18)</td>
						<td class="rf-dt-c">(19)</td>
						<td class="rf-dt-c">(20)</td>
						<td class="rf-dt-c">(21)= 240000x(20)</td>
						
						<td class="rf-dt-c">(22)=(15) +(16)+(17) +(18)+(19)+(21)</td>
						<td class="rf-dt-c">(23)</td>
						<!--<td class="rf-dt-c">(23)</td>
						 <td class="rf-dt-c">(24)</td> -->
						<td class="rf-dt-c">(24)=(22)-(23)</td>
						<td class="rf-dt-c"></td>
						<td class="rf-dt-c"></td>
					</tr>
					
					<?php 
						$sum_received=0;
						if(count($mem)>0){
							$vitri=0;
							foreach($mem as $khoa=>$giatri){
								?>
								<tr class="hang">
								
									<!-- <td class="rf-dt-c"><input type="checkbox" name="list" id="<?php echo "check".$khoa;?>" value="<?=$khoa?>"/></td> -->
									<td class="rf-dt-c"><?php echo ($vitri+1);?></td>
									<td class="rf-dt-c"><?php echo $giatri[name];?></td>
									<td class="rf-dt-c"><?php echo $giatri[position];?></td>
									<td class="rf-dt-c"><?php echo $giatri[really_number_time_work]."/".$giatri[number_time_work];?></td>
									<!-- <td class="rf-dt-c"><?php echo $giatri[number_allow_holiday];?></td>-->
									<td class="rf-dt-c"><?php echo number_format($giatri[basic_salary]);?></td>
									<td class="rf-dt-c"><?php echo number_format($giatri[allowance_basic_salary]);?></td>
									<td class="rf-dt-c"><?php echo $giatri[number_dateoff];?></td>
									<td class="rf-dt-c"><?php echo $giatri[number_holiday];?></td>
									<td class="rf-dt-c"><?php echo $giatri[number_is_comeback_early];?></td>
									<td class="rf-dt-c"><?php echo $giatri[number_is_late];?></td>
									
									<td class="rf-dt-c"><?php echo number_format($giatri[allowance_longyear]);?></td>
									
									<td class="rf-dt-c"><?php echo number_format($giatri[allowance_senior]);?></td>
									<td class="rf-dt-c"><?php echo number_format($giatri[allowance_c]);?></td>
									<td class="rf-dt-c"><?php echo number_format($giatri[oneday_salary]);?></td>
									<td class="rf-dt-c"><?php echo number_format($giatri[paid_salary]);?></td>
									<td class="rf-dt-c"><?php echo number_format($giatri[allowance_holiday]);?></td>
									<td class="rf-dt-c"><?php echo number_format($giatri[allowance_lunch]);?></td>
									<td class="rf-dt-c"><?php echo number_format($giatri[allowance_car]);?></td>
									<td class="rf-dt-c"><?php echo number_format($giatri[allowance_hard]);?></td>
									<td class="rf-dt-c"><?php echo $giatri[number_overtime];?></td>
									<td class="rf-dt-c"><?php echo number_format($giatri[overtime_salary]);?></td>
									
									<td class="rf-dt-c"><?php echo number_format($giatri[total_salary]);?></td>
									<td class="rf-dt-c"><?php echo number_format($giatri[insurance]);?></td>
									<!--<td class="rf-dt-c"><?php echo number_format($giatri[salary_bornus]);?></td>
									 <td class="rf-dt-c"><?php echo number_format($giatri[salary_sub]);?></td> -->
									<td class="rf-dt-c"><?php echo number_format($giatri[received_salary]);?></td>
									<td class="rf-dt-c"><?php echo $giatri[note];?></td>
									<td class="rf-dt-c"><?php echo $giatri[StartDate];?></td>
								</tr>
								<?php 
								$vitri++;
								$sum_received +=$giatri[received_salary];
							}
						}else{
							?>
							<tr><td colspan="27"><?php echo $l_list_emplty;?></td></tr>
							<?php 
						}
					?>
					<tr>
						<td colspan="23" class="rf-dt-c"></td>
						<td class="rf-dt-c" style="background: #93cddd;font-weight: bold;"><?php echo number_format($sum_received);?></td>
						<td class="rf-dt-c"></td>
						<td class="rf-dt-c"></td>
					</tr>
				</table> 
			</div>
			<div style="width:100%;align:center;" id="showtb">
				<input type="hidden" name="viewthongbao" id="viewthongbao" value="<?php echo $_SESSION['thongbao_employee_salary'];?>" />
				<?php 
				if(count($mem)>0 && $ktview==0){
					?>
					<table width="100%">
						<tr>
							<td align="center">
								<a href="content_salary/ajax_export_salary.php?year=<?=$_REQUEST[years]?>&month=<?=$_REQUEST[months]?>"><input type="button" name="btexcel" id="btexcel" value="<?php echo $l_export_excel?>"  class="btnupbg btnupload" /></a>
							</td>
						</tr>
					</table>
					<?php 
				}
				?>
			</div>
			<div style="margin-top: 15px; text-align: center; padding-right: 10px">
			<?php 
				if(count($mem)>0 && $ktview==1){
					?>
					<table width="100%">
						<!-- <tr>
							<td width="30px"><img src="images/bonus-icon.gif" width="30px" height="30px" ></td>
							<td class="tdfirst" style="text-align: left;"><?php echo $l_num_allow_holiday;?></td>
							<td style="text-align:left;"  >
								<input onkeypress="return enterTable(event,this.id,'1');" style="width:200px;height:24px" type="text" id="txtholiday" name="txtholiday" value="<?=$_REQUEST['txtbonus']?>" />
								<?php echo $l_note;?>
								<input onkeypress="return enterTable(event,this.id,'0');" style="width:200px;height:24px" type="text" id="txtnoteholiday" name="txtnoteholiday" value="<?=$_REQUEST['txtnote']?>" />
								<input type="button" name="btholidayok" onclick="holiday(1);" id="btholidayok" value="<?php echo $l_ok?>"  class="buttonProject" />
								<input type="button" name="btholidaycancel" onclick="holiday(0);" id="btholidaycancel" value="<?php echo $l_cancel?>"  class="buttonProject" />
							</td>
						</tr>
						<tr>
							<td width="30px"><img src="images/bonus-icon.gif" width="30px" height="30px" ></td>
							<td class="tdfirst" style="text-align: left;"><?php echo $l_bonus_holidays;?></td>
							<td style="text-align:left;"  >
								<input onkeypress="return enterTable(event,this.id,'1');" style="width:200px;height:24px" type="text" id="txtbonus" name="txtbonus" value="<?=$_REQUEST['txtbonus']?>" />
								<?php echo $l_note;?>
								<input onkeypress="return enterTable(event,this.id,'0');" style="width:200px;height:24px" type="text" id="txtnote" name="txtnote" value="<?=$_REQUEST['txtnote']?>" />
								<input type="button" name="btbonusok" onclick="bonus(1);" id="btbonusok" value="<?php echo $l_ok?>"  class="buttonProject" />
								<input type="button" name="btbonuscancel" onclick="bonus(0);" id="btbonuscancel" value="<?php echo $l_cancel?>"  class="buttonProject" />
							</td>
						</tr> -->
						<tr>
							<td colspan="3">
								<input type="button" name="btsave" onclick="save(<?=$_REQUEST[years]?>,<?=$_REQUEST[months]?>);" id="btsave" value="<?php echo $l_save?>"  class="btnupbg btnupload" />
							</td>
						</tr>
					</table>
					
					<?php 
				}
			?>
				
			</div>
			</div>
		</div>
	</div>
</div>
</form>